2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_spi_ex1_master.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //*****************************************************************************
74 //*****************************************************************************
75 #include "driverlib.h"
76 
77 uint8_t RXData = 0;
78 uint8_t TXData = 0;
79 
80 void main(void)
81 {
82  volatile uint16_t i;
83 
84  //Stop watchdog timer
85  WDT_A_hold(WDT_A_BASE);
86 
87  //Set P1.0 as an output pin.
88  /*
89 
90  * Select Port 1
91  * Set Pin 0 as output
92  */
93  GPIO_setAsOutputPin(
94  GPIO_PORT_P1,
95  GPIO_PIN0
96  );
97 
98  //Set P1.0 as Output Low.
99  /*
100 
101  * Select Port 1
102  * Set Pin 0 to output Low.
103  */
105  GPIO_PORT_P1,
106  GPIO_PIN0
107  );
108 
109  // XT1 Setup
110  //Set P4.1 and P4.2 as Function Input.
111  /*
112 
113  * Select Port 4
114  * Set Pin 1, 2 to input Module Function, XT1.
115  */
116  GPIO_setAsPeripheralModuleFunctionInputPin(
117  GPIO_PORT_P4,
118  GPIO_PIN1 + GPIO_PIN2,
119  GPIO_PRIMARY_MODULE_FUNCTION
120  );
121 
122  //Set external frequency for XT1
123  CS_setExternalClockSource(32768);
124 
125  //Select XT1 as the clock source for SMCLK with no frequency divider
126  CS_initClockSignal(CS_SMCLK, CS_XT1CLK_SELECT, CS_CLOCK_DIVIDER_1);
127 
128  //Start XT1 with no time out
129  CS_turnOnXT1(CS_XT1_DRIVE_0);
130 
131  // Configure SPI pins
132  // Configure Pins for UCB0CLK, UCB0TXD/UCB0SIMO and UCB0RXD/UCB0SOMI
133  /*
134 
135  * Select Port 5
136  * Set Pin 1, Pin 2 and Pin 3 to input with function.
137  */
138  GPIO_setAsPeripheralModuleFunctionInputPin(
139  GPIO_PORT_P5,
140  GPIO_PIN1 + GPIO_PIN2 + GPIO_PIN3,
141  GPIO_PRIMARY_MODULE_FUNCTION
142  );
143 
144  /*
145  * Disable the GPIO power-on default high-impedance mode to activate
146  * previously configured port settings
147  */
148  PMM_unlockLPM5();
149 
150  //Initialize Master
151  EUSCI_B_SPI_initMasterParam param = {0};
152  param.selectClockSource = EUSCI_B_SPI_CLOCKSOURCE_SMCLK;
153  param.clockSourceFrequency = CS_getSMCLK();
154  param.desiredSpiClock = 500000;
155  param.msbFirst = EUSCI_B_SPI_MSB_FIRST;
156  param.clockPhase = EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
157  param.clockPolarity = EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
158  param.spiMode = EUSCI_B_SPI_3PIN;
159  EUSCI_B_SPI_initMaster(EUSCI_B0_BASE, &param);
160 
161  //Enable SPI module
162  EUSCI_B_SPI_enable(EUSCI_B0_BASE);
163 
164  EUSCI_B_SPI_clearInterrupt(EUSCI_B0_BASE,
165  EUSCI_B_SPI_RECEIVE_INTERRUPT
166  );
167 
168  // Enable USCI_B0 RX interrupt
169  EUSCI_B_SPI_enableInterrupt(EUSCI_B0_BASE,
170  EUSCI_B_SPI_RECEIVE_INTERRUPT);
171 
172  //Wait for slave to initialize
173  __delay_cycles(100);
174 
175  TXData = 0x1; // Holds TX data
176 
177  //USCI_B0 TX buffer ready?
178  while (!EUSCI_B_SPI_getInterruptStatus(EUSCI_B0_BASE,
179  EUSCI_B_SPI_TRANSMIT_INTERRUPT)) ;
180 
181  //Transmit Data to slave
182  EUSCI_B_SPI_transmitData(EUSCI_B0_BASE, TXData);
183 
184  __bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
185  __no_operation(); // Remain in LPM0
186 }
187 
188 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
189 #pragma vector=USCI_B0_VECTOR
190 __interrupt
191 #elif defined(__GNUC__)
192 __attribute__((interrupt(USCI_B0_VECTOR)))
193 #endif
194 void USCI_B0_ISR (void)
195 {
196  switch (__even_in_range(UCB0IV, USCI_SPI_UCTXIFG))
197  {
198  case USCI_SPI_UCRXIFG: // UCRXIFG
199  //USCI_B0 TX buffer ready?
200  while (!EUSCI_B_SPI_getInterruptStatus(EUSCI_B0_BASE,
201  EUSCI_B_SPI_TRANSMIT_INTERRUPT));
202 
203  RXData = EUSCI_B_SPI_receiveData(EUSCI_B0_BASE);
204 
205  //Increment data
206  TXData++;
207 
208  //Send next value
209  EUSCI_B_SPI_transmitData(EUSCI_B0_BASE,
210  TXData
211  );
212 
213  //Delay between transmissions for slave to process information
214  __delay_cycles(40);
215  break;
216  default:
217  break;
218  }
219 }
220 
uint8_t RXData
void main(void)
void USCI_B0_ISR(void)
uint8_t TXData
MPU_initThreeSegmentsParam param
__no_operation()
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)